package com.happyfamily.springboot.crowdsourcingplatform.controller.pan;

import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.PageTableRequest;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.ResponseCode;
import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dto.LoginBackUser;
import com.happyfamily.springboot.crowdsourcingplatform.dto.UserDto;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.Need;
import com.happyfamily.springboot.crowdsourcingplatform.model.ServerType;
import com.happyfamily.springboot.crowdsourcingplatform.model.SysUser;
import com.happyfamily.springboot.crowdsourcingplatform.service.pan.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author Administrator
 * @create 2020/02/15 15:27
 */
@Controller
@Slf4j
public class UserController
{

	@Autowired
	private UserService userService;


	/**
 * 跳转新增用户页面
 *
 * @param
 * @return
 */
@GetMapping("user/add")
//	@PreAuthorize("hasAuthority('sys:user:add')")
//	@ApiOperation(value = "用户新增页面", notes = "跳转到新增用户信息页面")//描述
public String addUser(Model model)
{
	model.addAttribute(new SysUser());
	return "frontPage/user-reg";

}


	/**
	 * 新增用户
	 *
	 * @param
	 * @return
	 */
	@PostMapping("/user/add")
	@ResponseBody
	//控制授权
	//	@PreAuthorize("hasAuthority('sys:user:add')")
	@ApiOperation(value = "保存用户信息", notes = "保存新增的用户信息", httpMethod = "POST", response = Results.class)
	@OperationLogDetail(detail = "保存用户信息", level = 3, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<SysUser> saveUser(UserDto userDto, Integer rid, Integer sid)
	{

		SysUser user = null;
		user = userService.getUserByPhone(userDto.getUphone());
		if (user != null && !(user.getUid().equals(userDto.getUid())))
		{
			return Results.failure(ResponseCode.PHONE_REPEAT.getCode(), ResponseCode.PHONE_REPEAT.getMessage());
		}
		user = userService.getUser(userDto.getUaccount());
		if (user != null && !(user.getUid().equals(userDto.getUid())))
		{
			return Results.failure(ResponseCode.USERNAME_REPEAT.getCode(), ResponseCode.USERNAME_REPEAT.getMessage());
		}


		//spring security的密码加密
		userDto.setUpassword(new BCryptPasswordEncoder().encode(userDto.getUpassword()));
		return userService.save(userDto, rid, sid);


	}


	/**
	 * 跳转用户发布需求页面
	 *
	 * @param
	 * @return
	 */
	@GetMapping("api-front/user/publishing")
	public String UserPublishing(Model model)
	{
//
		model.addAttribute(new SysUser());
		return "frontPage/publishing";

	}


	/**
	 * 获取父级sparentid
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("api-front/user/sparentid")
	@ResponseBody
	@ApiOperation(value = "服务类型获取", notes = "查询类型是服务商类型", httpMethod = "GET", response = List.class)
	@OperationLogDetail(detail = "服务类型获取", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public List<ServerType> Type(HttpServletRequest request) throws Exception
	{
		List<ServerType> list = userService.findById(0);
		//		将非服务商移除
		for (int i = 0; i < list.size(); i++)
		{
			if (list.get(i).getSid() == 1)
			{
				list.remove(i);
			}
		}

		return list;
	}

	/**
	 * 获取父级sparentid
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("user/service")
	@ResponseBody
//	@ApiOperation(value = "服务商类型获取", notes = "查询类型是服务商类型", httpMethod = "GET", response = List.class)
//	@OperationLogDetail(detail = "服务商类型获取", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public List<ServerType> serviceType(HttpServletRequest request) throws Exception
	{
		List<ServerType> list = userService.findById(-1);
		System.out.println(list);
		return list;
	}






















	/**
	 * 根据父级sparentid获取子级
	 *
	 * @param request
	 * @param Id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("api-front/user/sony")
	@ResponseBody
	@ApiOperation(value = "服务商类型子级数据获取", notes = "查询类型是服务商类型", httpMethod = "GET", response = Results.class)
	@OperationLogDetail(detail = "服务商类型子级数据获取", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public List<ServerType> Status(HttpServletRequest request, Long Id) throws Exception
	{

		List<ServerType> Status = userService.Status(Id);
		return Status;
	}


	/**
	 * 雇主发布需求
	 *
	 * @param
	 * @return
	 */
	@PostMapping("api-front/user/pub")
	@ResponseBody
	@ApiOperation(value = "保存发布需求的信息", notes = "保存的信息是发布需求", httpMethod = "POST", response = Results.class)
	@OperationLogDetail(detail = "保存发布需求的信息", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public Results<Need> saveUserPublishing(Need need, HttpServletRequest request)
	{

		SecurityContextImpl securityContextImpl = (SecurityContextImpl)

				request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		//获得你登录的账号
		String account = securityContextImpl.getAuthentication().getName();
		//判断用户是否登录
		if (account == null)
		{

			return Results.failure(ResponseCode.USER_LOGIN_REPEAT.getCode(), ResponseCode.USER_LOGIN_REPEAT.getMessage());

		}
		//保存用户的需求
		return userService.savePublishing(need, account);


	}

	/**
	 * 跳转我的需求列表页面
	 *
	 * @param
	 * @return
	 */
	@GetMapping("api-front/user/userNeeds")
	public String UserNeeds(Model model)
	{
		model.addAttribute(new SysUser());
		return "frontPage/myneeds";

	}

















}
